﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Cofe.Core;
using Cofe.Core.Interfaces;
using Cofe.Core.Logger;
using Cofe.Web.Utils;

namespace Cofe.Web.Feed
{
    /// <summary>
    /// EventArgsFeedService, for testing only.
    /// </summary>
    #region EventArgsFeedService

    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class EventArgsFeedService : FeedServiceBase<EventArgs>
    {
        public EventArgsFeedService()
            : base()
        {
            this.FileSystem = new FeedFileSystemHelper(@"C:\temp\log\current", @"C:\temp\log\archive", @"C:\temp\log\entry");
            this.Builder = new EventArgsFeedBuilder(this)
                {
                    FeedTemplate = new UriTemplate("eventArgs/{id}"),
                    EventTemplate = new UriTemplate("entry/{id}")
                };
            //Repository/Links/Updater are set in base class.
        }
    }

    #endregion

    #region EventArgsFeedBuilder

    public class EventArgsFeedBuilder : FeedBuilderBase<EventArgs>
    {
        #region Constructor


        public EventArgsFeedBuilder(IFeedService<EventArgs> feedService)
            : base(feedService)
        {
            ServiceName = "EventArgs Builder";
            Title = "Testing Feed Implementation";
        }

        #endregion

        #region Methods

        public override FeedEntry<EventArgs> BuildEntry(IEventLog<EventArgs> eventLog)
        {
            var eventLogId = Guid.NewGuid(); /*Test event Id */
            SyndicationItem item = new SyndicationItem
            {
                Id = string.Format("tag:testFeed,{0}:{1}", eventLog.UtcTime.ToString("yyyy-MM-dd"), eventLogId),
                Title = SyndicationContent.CreatePlaintextContent(eventLog.EventArgs.ToString()),
                LastUpdatedTime = eventLog.UtcTime
            };

            item.Links.Add(_feedService.Links.CreateEventLink(LinkType.self, eventLog, eventLogId));
            //item.Links.Add(_feedService.Links.CreateEntryLink(LinkType.via, eventLog, eventLogId));
            //item.Content = new XmlSyndicationContent("application/xml", evnt.Body.Payload, null as DataContractSerializer);

            return new FeedEntry<EventArgs>(item, eventLogId + ".atom");
        }

        #endregion

        #region Data

        #endregion

        #region Public Properies



        #endregion

    }

    #endregion

   

}
